# -*- coding: UTF-8 -*-
from leetcode_helper import TestcaseHelper, SolutionBase


class Solution(SolutionBase):
    def lengthOfLongestSubstring(self, s: str) -> int:
        longest_length = 0
        temp_dict = {}
        temp_length = 0

        start_index = 0

        for index, char in enumerate(s):
            exist_index = temp_dict.get(char, -1)

            if exist_index != -1 and exist_index >= start_index:
                temp_dict[char] = index
                temp_length = index - exist_index
                start_index = exist_index + 1
            else:
                temp_dict[char] = index
                temp_length += 1
                if longest_length < temp_length:
                    longest_length = temp_length

        return longest_length


if __name__ == '__main__':
    solution = Solution()
    testcases = [
        ('abcabcbb', 3),
        ('bbbbb', 1),
        ('pwwkew', 3),
        (' ', 1),
        ('au', 2),
        ('aab', 2)
    ]

    for case_item in testcases:
        # 获取测试用的case和期待的执行结果
        # TODO: 单独一条的Test case的组成随题目的不同而改变，需要按照题目来修改代码
        input1 = case_item[0]
        expect = case_item[len(case_item) - 1]
        # TODO: 调用对应方法，需要替换具体的方法
        exec_result = solution.lengthOfLongestSubstring(input1)

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)


